<?php
class SiteSaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('site','index');
	}
	
	public function execute()
	{
		//验证APP Key和Secret个数是否一致
		$keys		= explode(',',$this->context->post('taobao_api_appkey'));
		$secrets	= explode(',',$this->context->post('taobao_api_appsecret'));
		
		if(count($keys) != count($secrets))
		{
			$this->data['error_messages']['taobao_api_appsecret']	= array('equal'=>'APP Key和Secret个数不一致');
		}
		
		//验证淘宝会员名和pid必须至少填写一个
		$nick	= $this->context->post('taobao_api_nick');
		$pid	= $this->context->post('taobao_api_pid');
		if(empty($nick) && empty($pid))
		{
			$this->data['error_messages']['taobao_api_nick']	= array('equal'=>'淘宝网会员账号和阿里妈妈PID至少填写一个');
		}
		
		if(!empty($this->data['error_messages']))
		{
			$this->code	= 407;
		}
		else
		{
			$data	= $this->getValidData();
			$data['taobao_api_num']	= count($keys);
			$settingService	= new SettingService();
			$skey			= 'api_setting';
			$tmp			= $settingService->get($skey);
			if(!$tmp)
			{
				$settingService->add($skey,$data,true);
			}
			else
			{
				$settingService->update($skey,$data,false,true);
			}
			
			$this->code	= 200;
		}
	}
}
